﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agr.CQRS.Infrastructure.Orm;

namespace Agr.CQRS.ViewModel
{
    public class ViewModelQueryFactory : IDisposable
    {
        OrmContext ormContext;
        SessionInTransaction session;

        public ViewModelQueryFactory(OrmContext ormContext)
        {
            this.ormContext = ormContext;
        }

        private SessionInTransaction getSession()
        {
            if (session == null)
                session = ormContext.OpenReadonlySession();
            return session;
        }

        public T CreateQuery<T>() where T:IOrmSessionAware, new()
        {
            return new T { Tx = getSession() };
        }

        public void Dispose()
        {
            if (session != null)
                session.Dispose();
        }
    }
}
